﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Nizm0.QOT.LocalStorage
{
    [XmlRoot("LocalStorageSettings")]
    public sealed class LocalStorageSettings : IXmlSerializable
    {
        private static object syncRoot = new Object();

        private static volatile LocalStorageSettings _instance;
        public static LocalStorageSettings Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (syncRoot)
                    {
                        if (_instance == null)
                        {
                            _instance = new LocalStorageSettings();
                            LocalStorageSettings.Instance.Load();
                        }
                    }
                }

                return _instance;
            }
        }

        private bool _use = false;
        public bool Use
        {
            get { return _use; }
            set { _use = value; }
        }

        private string _fileSettingsLocation;
        public string FileSettingsLocation
        {
            get { return _fileSettingsLocation; }
            //set { _fileSettingsLocation = value; }
        }

        private string _fileName = "LocalStorageSettings.config";

        private string _fileSettingsName = "LocalStorage.settings";
        public string FileSettingsName
        {
            get { return _fileSettingsName; }
            //set { _fileSettingsName = value; }
        }

        private string _serverAdress;
        public string ServerAdress
        {
            get { return _serverAdress; }
            set { _serverAdress = value; }
        }

        private bool _inherit = true;
        public bool Inherit
        {
            get { return _inherit; }
            set { _inherit = value; }
        }
        /// <summary>
        /// Location of storage container files
        /// </summary>
        private string _location = Settings.Instance.ProgramLocalization;
        public string Location
        {
            get { return _location; }
            set { _location = value; }
        }

        private LocalStorageSettings()
        {
        }

        private void Load()
        {
            try
            {
                if (!System.IO.File.Exists(Settings.Instance.SettingsLocation + "\\" + _fileName)) Save();
                XmlSerializer xmlSerial = new XmlSerializer(this.GetType());
                using (System.IO.StreamReader str = new System.IO.StreamReader(Settings.Instance.SettingsLocation + "\\" + _fileName))
                {
                    //str.ReadToEnd();
                    //str.po
                    LocalStorageSettings obj = (LocalStorageSettings)xmlSerial.Deserialize(str);
                    this._location = obj._location;
                    this._serverAdress = obj._serverAdress;
                    str.Close();
                }
            }
            catch (Exception ex)
            {
            }
        }

        public bool Save()
        {
            try
            {
                XmlSerializer xmlSerial = new XmlSerializer(this.GetType());
                using (System.IO.StreamWriter str = new System.IO.StreamWriter(Settings.Instance.SettingsLocation + "\\" + _fileName, false))
                {
                    xmlSerial.Serialize(str, this);
                    //str.Write(str);
                    str.Close();
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            throw new NotImplementedException();
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            bool.TryParse(reader.GetAttribute("Use"), out _use);
            _location = reader.GetAttribute("Location");
            _serverAdress = reader.GetAttribute("ServerAdress");
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteAttributeString("Use", _use.ToString());
            writer.WriteAttributeString("Location", _location);
            writer.WriteAttributeString("ServerAdress", _serverAdress);
        }
    }
}
